package com.financing.platform.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.financing.platform.dao.IFaUserDao;
import com.financing.platform.dao.IFpFundInfoDao;
import com.financing.platform.dao.IOrderTmpDao;
import com.financing.platform.dao.IProjectDao;
import com.financing.platform.dto.FaUserDTO;
import com.financing.platform.dto.FpFundInfoDTO;
import com.financing.platform.dto.OrderTmpDTO;
import com.financing.platform.dto.ProjectDTO;
import com.financing.platform.service.IOrderTmpService;
import com.financing.platform.util.StringUtil;

/**
 * 
* @ClassName: OrderServiceImpl 
* @Description: 订单流程处理 
* @author chenming 
* @date 2014年8月18日 下午2:42:30 
*
 */
@Service("orderTmpServiceImpl")
@Transactional
public class OrderTmpServiceImpl extends BaseServiceImpl implements IOrderTmpService {

	@Autowired
	private IOrderTmpDao orderTmpDaoImpl;
	
	@Autowired
	private IProjectDao projectDaoImpl;
	
	@Autowired
	private IFaUserDao faUserDaoImpl;
	
	@Autowired
	private IFpFundInfoDao fpFundInfo;
	
	@Override
	public List<Map<String,Object>> queryOrderTmpList(OrderTmpDTO orderTmpDto)
			throws Exception {
		List<Map<String,Object>> res = new ArrayList<Map<String,Object>>();
		  
		List<OrderTmpDTO> orderTmpList = orderTmpDaoImpl.queryOrderTmpList(orderTmpDto);
		
		
		for(OrderTmpDTO orderTmp : orderTmpList){
			Map<String,Object> resMap = new HashMap<String,Object>();
			resMap.put("orderTmp", orderTmp);
			
			FaUserDTO faUser = new FaUserDTO();
			faUser.setId(orderTmp.getFaId());
			faUser = faUserDaoImpl.getFaUser(faUser);
			if(faUser==null){//null exception处理
				continue;
			}
			faUser.setuPassWord(null);
			resMap.put("faInfo", faUser);
			
			if("01".equals(orderTmp.getoType())){
				ProjectDTO projectDTO = new ProjectDTO();
				projectDTO.setId(orderTmp.getProductId());
				resMap.put("product", projectDaoImpl.getProjectList(projectDTO));
			}else if("02".equals(orderTmp.getoType())){
				FpFundInfoDTO ffi = new FpFundInfoDTO();
				ffi.setId(orderTmp.getProductId());
				resMap.put("product",fpFundInfo.getFpFundInfo(ffi));
			}
			res.add(resMap);
		}
		
		return res;
	}

	@Override
	public int queryOrderTmpCount(OrderTmpDTO orderTmpDto) throws Exception {
		return orderTmpDaoImpl.queryOrderTmpCount(orderTmpDto);
	}

	@Override
	public int updateOrderTmp(OrderTmpDTO orderTmpDto) throws Exception {
		return orderTmpDaoImpl.updateOrderTmp(orderTmpDto);
	}

	@Override
	public List<Map<String, Object>> queryOrderTmpByProIdList(
			OrderTmpDTO orderTmpDto) throws Exception {
		return orderTmpDaoImpl.queryOrderTmpByProidList(orderTmpDto);
	}

	@Override
	public int countOrderTmpByProIdList(
			OrderTmpDTO orderTmpDto) throws Exception {
		return orderTmpDaoImpl.countOrderTmpByProidList(orderTmpDto);
	}
	
	@Override
	public List<OrderTmpDTO> queryInfoByOrderTmpIds(String orderTmpIds) throws SQLException{
		//处理字符串
		orderTmpIds=StringUtil.splitToIn(orderTmpIds);
		
		return orderTmpDaoImpl.queryInfoByOrderTmpIds(orderTmpIds);
	}

	@Override
	public List<OrderTmpDTO> queryOrderTmpByList(OrderTmpDTO orderTmpDto)
			throws Exception {
		return orderTmpDaoImpl.queryOrderTmpList(orderTmpDto);
	}
}